﻿<UserControl
    x:Class="ViewSwitchingNavigation.Calendar.Views.CalendarView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
    xmlns:infrastructure="clr-namespace:ViewSwitchingNavigation.Infrastructure;assembly=ViewSwitchingNavigation.Infrastructure"
    xmlns:cm="clr-namespace:System.ComponentModel;assembly=System.Windows"
    xmlns:data="clr-namespace:System.Windows.Data;assembly=System.Windows"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="650" >

    <UserControl.Resources>
        <CollectionViewSource x:Key="CalendarEntries" Source="{Binding Meetings}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="StartTime.Date" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

        <infrastructure:ObservableCommand x:Key="OpenMeetingEmailCommand" Value="{Binding OpenMeetingEmailCommand}"/>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">

        <DataGrid ItemsSource="{Binding Source={StaticResource CalendarEntries}}" AutoGenerateColumns="False" IsReadOnly="True"
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutomationProperties.AutomationId="CalendarGrid"
                  HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto">

            <DataGrid.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Path=Name}" />
                            </StackPanel>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander Background="LightGray" HorizontalAlignment="Stretch" IsExpanded="True">
                                            <Expander.Header>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Text="{Binding Path=Name}" Margin="10,0,10,0" />
                                                    <TextBlock>(</TextBlock>
                                                    <TextBlock Text="{Binding Path=ItemCount}" Margin="0,0,5,0"/>
                                                    <TextBlock Text="Items"/>
                                                    <TextBlock>)</TextBlock>
                                                </StackPanel>
                                            </Expander.Header>
                                            <Grid HorizontalAlignment="Stretch">
                                                <ItemsPresenter HorizontalAlignment="Stretch" />
                                            </Grid>
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </DataGrid.GroupStyle>
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Subject" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Stretch">
                                <Hyperlink Command="{Binding DataContext.OpenMeetingEmailCommand, ElementName=LayoutRoot}" CommandParameter="{Binding}"
                                           AutomationProperties.AutomationId="OpenMailHyperLink">
                                    <TextBlock Text="{Binding Subject}" />
                                </Hyperlink>
                            </TextBlock>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Start" MinWidth="150" Binding="{Binding StartTime, StringFormat=g}" CanUserSort="False" Foreground="Black"/>
                <DataGridTextColumn Header="End" MinWidth="150" Binding="{Binding EndTime, StringFormat=g}" CanUserSort="False" Foreground="Black"/>
            </DataGrid.Columns>

        </DataGrid>
    </Grid>
</UserControl>
